/**
 *@auther: tang
 *@Date: 2020-12-02
 *Project: demo
 */
import {makeObservable,action,observable} from "mobx"
import getCrTime from "../../util/getNowTime";
class TodoData {
    constructor() {
        makeObservable(this);
    }
    @observable
        completedList = []
        activeList = []
    get completedList(){
        return sessionStorage.getItem("todoComp")?JSON.parse(sessionStorage.getItem("todoComp")):this.completedList;
    }
    set completedList(val){
        sessionStorage.setItem("todoComp",JSON.stringify(val));
        return ""
    }
    get activeList(){
        return sessionStorage.getItem("todoActive")?JSON.parse(sessionStorage.getItem("todoActive")):this.activeList;
    }
    set activeList(val){
        sessionStorage.setItem("todoActive",JSON.stringify(val));
        return ""
    }
    @action
        getDataById(id){
            switch (id){
                case 0:
                    return this.getAllList();
                case 1:
                    return this.completedList;
                case 2:
                    return this.activeList;
                default:
                    return ""
            }
        }
        getAllList(){
            return [...this.completedList,...this.activeList];
        }
        //默认添加一个待办事项
        addDataById(id,val){
            switch (id){
                case 1:
                    let comp = this.completedList;
                    comp.push(val);
                    this.completedList = comp;
                    break;
                case 2:
                    let active = this.activeList;
                    active.push(val);
                    this.activeList = active;
                    break;
                default:
                    return ""
            }
        }
        //完成一个事项的状态
        compOne(itemObj){
            this.activeList.forEach((item,index)=>{
                if (item.id===itemObj.id){
                    item.crDate = getCrTime();
                    item.isDid =true;
                       let comp = this.completedList;
                       comp.push(item);
                        this.completedList = comp;
                        let old = [...this.activeList];
                        old.splice(index,1)
                        this.activeList = old;
                }
            })
        }
        cancelOne(itemObj){
            this.completedList.forEach((item,index)=>{
                if (item.id===itemObj.id){
                    item.isDid = false;
                    item.crDate = getCrTime();
                        let active = this.activeList;
                        active.push(item);
                        this.activeList = active;
                        let old = [...this.completedList];
                        old.splice(index,1)
                        this.completedList = old;
                }
            })
        }
}

export default new TodoData();
